//
//  TMGMessageListViewController.m
//  TMGold
//
//  Created by Mac on 2018/3/19.
//  Copyright © 2018年 tangmu. All rights reserved.
//

#import "TMGMessageListViewController.h"
#import "TMGSystemMessageTableViewCell.h"
#import "TMGMessagedetailViewController.h"
@interface TMGMessageListViewController ()
@property(nonatomic, strong) NSMutableArray<TMGSystemMessageModel*>                     *dataSouce;
@end

@implementation TMGMessageListViewController

#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"通知消息";
    self.tableView.frame = CGRectMake(0, 0, UIScreenWidth, [TMUtils tabBarTop] - [TMUtils navgationBarBootom]);
    self.tableView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 400) title:@"暂无系统消息" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendNotifcationRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self st_setRightItemWithTitle:@"在线客服" titleColor:FlatWhite];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1   ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [TMGSystemMessageTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    TMGSystemMessageTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[TMGSystemMessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    TMGSystemMessageModel * model = self.dataSouce[indexPath.row];
    cell.model = model;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TMGSystemMessageModel * model = self.dataSouce[indexPath.row];
    TMGMessagedetailViewController * vc = [TMGMessagedetailViewController new];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)st_rightBarAction:(id)sender{
    [TMUtils gotoCustomServiceViewController];
}
#pragma mark --NetWork Method
- (void)sendNotifcationRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/personal/get_message_list"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSArray * array = responseObject[@"data"][@"data"];
                                                 self.dataSouce = [NSMutableArray new];
                                                 for (NSDictionary *modelDic in array) {
                                                     TMGSystemMessageModel * model = [TMGSystemMessageModel mj_objectWithKeyValues:modelDic];
                                                     [self.dataSouce addObject:model];
                                                 }
                                                 [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView.mj_header endRefreshing];
                                             }];
}
@end
